package one.stand.controller;

import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.SecurityLoginIpadPwdRequest;
import com.jmp.base.dto.SecurityLoginIpadWxRequest;
import com.jmp.base.dto.SunCodeCardRequest;
import com.jmp.base.dto.SunCodeCardResponse;
import com.jmp.feign.SecurityFeign;
import com.jmp.feign.SunCodeFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.advice.ResultEnum;
import one.stand.dto.LoginConfigResponse;
import one.stand.dto.LoginRequest;
import one.stand.dto.LoginResponse;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.model.SysDictValue;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.sys.SysDictValueService;
import one.stand.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author cjr
 */
@Api(description = "登录登出")
@RestController
public class LoginController {
    @Autowired
    private SunCodeFeign sunCodeFeign;
    @Autowired
    private SecurityFeign securityFeign;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private SysDictValueService sysDictValueService;

    @ApiOperation("配置")
    @PostMapping("/config")
    public Result<LoginConfigResponse> config() {
        SysDictValue sysDictValue = sysDictValueService.get(SysDictValueService.Type.IPAD_CONFIG, SysDictValueService.TypeName.IPAD_WXLOGIN);
        Integer wxLogin = Objects.nonNull(sysDictValue) ? Integer.parseInt(sysDictValue.getValue()) : 1;
        LoginConfigResponse response = LoginConfigResponse.builder()
                .wxLogin(wxLogin)
                .build();
        return ResultUtil.success(response);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<LoginResponse> login(LoginRequest request) {
        ResultModel<one.stand.response.LoginResponse> result;
        if (Objects.isNull(request.getPassword())) {
            RequestContext requestContext = RequestContextHolder.getRequestContext();
            String equCode = requestContext.getRequest().getEquCode();
            SecurityLoginIpadWxRequest wxRequest = SecurityLoginIpadWxRequest.builder()
                    .code(request.getUsername())
                    .equCode(equCode)
                    .build();
            result = securityFeign.loginIpadWx(wxRequest);
        } else {
            SecurityLoginIpadPwdRequest pwdRequest = SecurityLoginIpadPwdRequest.builder()
                    .username(request.getUsername())
                    .password(request.getPassword())
                    .build();
            result = securityFeign.loginIpadPwd(pwdRequest);
        }

        AssertUtil.isTrue(result.isSuccess(), result.getMessage(), ResultEnum.IPAD_FAIL_LOGIN.getCode());

        SunCodeCardRequest sunCodeCardRequest = SunCodeCardRequest.builder().cardId(result.getData().getCardId()).build();
        ResultModel<SunCodeCardResponse> sunCodeCardResponseResultModel = sunCodeFeign.card(sunCodeCardRequest);
        AssertUtil.isTrue(sunCodeCardResponseResultModel);
        String wxCode = sunCodeCardResponseResultModel.getData().getCodeUrl();

        BusinessCard businessCardModel = businessCardBaseService.getCustomer(result.getData().getCardId());

        LoginResponse loginResponse = LoginResponse.builder()
                .accessToken(result.getData().getToken())
                .name(result.getData().getUserName())
                .icon(result.getData().getAvatarUrl())
                .wxCode(wxCode)
                .position(businessCardModel.getPosition())
                .build();
        return ResultUtil.success(loginResponse, result.isSuccess());
    }

    @ApiOperation("登出")
    @GetMapping("/logout")
    @SecurityAuthCheck(company = true)
    public Result logout() {
        return ResultUtil.success();
    }
}
